package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.Comment;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author ritesh gupta
 * This class is for managing user comments like insertion, modification
 *
 */
public class CommentDao extends DatabaseDAO{
	
	/**to fetch all the comments of a particular status 
	 * @param statusId  id of the status 
	 * @return  the array of comments
	 * @throws SQLException
	 */
	public List<Comment> getRecordList (int statusId)
	throws SQLException {
		ArrayList<Comment> commentList = null;
		String query = "Select * from comment where status_id = ? order by comment_timestamp"; // query
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, statusId);
			rs = ps.executeQuery();
			commentList = new ArrayList<Comment>();
			while (rs.next()) {
				Comment comment = fillBean();
				commentList.add(comment);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return commentList;
	}
	
	/**to fill the bean of comments 
	 * @return  the comment object
	 * @throws SQLException
	 */
	private Comment fillBean() throws SQLException{
		Comment comment = new Comment();
		comment.setCommentId(rs.getInt("comment_id"));  // set commentid
		comment.setStatusId(rs.getInt("status_id"));  // set status id
		comment.setUserId(rs.getInt("user_id"));  // set userid
		comment.setComment(rs.getString("comment"));  // set comment
		comment.setCommentTimestamp(rs.getTimestamp("comment_timestamp"));  // set timestamp
		return comment;
	}
	
	/**to retrieve a particular comment
	 * @param commentId
	 * @return comment object
	 * @throws SQLException
	 */
	public Comment getRecord(int commentId)
	throws SQLException {
		String query = "Select * from comment where comment_id = ? ";  // query
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, commentId);
			rs = ps.executeQuery();
			if (rs.next()) {
				
				return fillBean();
			}
			return null;
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**to add a comment
	 * @param comment to be inserted
	 * @return  integer
	 * @throws SQLException
	 */
	public int addRecord(Comment comment) throws SQLException {
		if(!checkIsPresent(comment.getCommentId())){
			return insertRecord(comment);
		}
		else{
			return updateRecord(comment);
		}
	}
	
	/**to update a comment
	 * @param comment comment to be updated
	 * @return  integer
	 * @throws SQLException
	 */
	private int updateRecord(Comment comment) throws SQLException {
		// TODO Auto-generated method stub
		getConnection();
		String query = "update comment set status_id =?, user_id =?, comment=? where comment_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, comment.getStatusId());
			ps.setInt(2, comment.getUserId());
			ps.setString(3, comment.getComment());
			ps.setInt(4, comment.getCommentId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}

	/** to insert a new comment
	 * @param comment  comment to be inserted
	 * @return  integer
	 * @throws SQLException
	 */
	private int insertRecord(Comment comment) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "insert into comment (status_id, user_id, comment) values(?, ? , ?)"; // query
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, comment.getStatusId());
			ps.setInt(2, comment.getUserId());
			ps.setString(3, comment.getComment());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}

	/**to check if a particular comment is present in the database.
	 * @param commentId id of he comment to be searched
	 * @return  true or false
	 * @throws SQLException
	 */
	public boolean checkIsPresent(int commentId) throws SQLException {
		getConnection();
		String query = "select 1 from comment where comment_id=?";  // query
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, commentId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			return rs.next();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**to delete a comment
	 * @param commentId id of the comment to be deleted
	 * @return integer.
	 * @throws SQLException
	 */
	public int deleteRecord(int commentId) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "delete from comment where comment_id =?"; // query.
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, commentId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}
	
}
